package com.ctsi.bigdata.todoitem.service;

import ch.qos.logback.classic.spi.IThrowableProxy;
import com.ctsi.bigdata.todoitem.dao.mapper.ItemMapper;
import com.ctsi.bigdata.todoitem.dao.pojo.Item;
import com.ctsi.bigdata.todoitem.dao.pojo.ItemExample;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by admin on 2015/11/18.
 */
@Service("itemService")
public class ItemService {
    @Autowired
    private ItemMapper itemMapper;

    public List<Item> itemList() {

        List<Item> list= null;
        try {
            list = itemMapper.selectByExample(new ItemExample());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    public List<Item> items(int pageNo,int pageSize){
        RowBounds rowBounds=new RowBounds(pageNo,pageSize);
        List<Item> items=null;
        try {
            items=itemMapper.selectByExampleWithRowbounds(new ItemExample(),rowBounds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return items;
    }

    public int addItem(Item item) {
        int flag=0;
        try {
            flag=itemMapper.insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public int deleteItem(Long itemId) {

        int flag=0;
        try {
            flag=itemMapper.deleteByPrimaryKey(itemId);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public List<Item> searchByItemName(String itemName) {
        ItemExample example=new ItemExample();
        example.or().andItemNameEqualTo(itemName);
        List<Item> items=null;
        try {
            items=itemMapper.selectByExample(example);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return items;
    }
}
